#include "widget.h"
#include "ui_widget.h"
#include<QRandomGenerator>
#include<QDebug>
#include<QFont>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    //窗口属性
    this->setFixedSize(800,800);
    this->setWindowTitle("贪吃蛇");//窗口标题
    this->setWindowIcon(QIcon(":/ziyuan/C:/Users/Dell/Desktop/source/snake.png"));

    //背景图
    QPixmap pixmap=QPixmap(":/ziyuan/C:/Users/Dell/Desktop/source/bg.png");
    QPalette palette;
    palette.setBrush(QPalette::Window,QBrush (pixmap));
    this->setPalette(palette);

    //得分标签
    QFont f("文本",30);
    scorelabel1->setFont(f);
    scorelabel1->setText("得分");


    //创建定时器
    timer = new QTimer();
    //信号槽
    connect(timer,SIGNAL(timeout()),this,SLOT(timeout()));
    connect(this->e->quitbt,SIGNAL(click()),this,SLOT(hide()));
    connect(this->e->againbt,SIGNAL(click()),this,SLOT(hide()));
    //蛇
    QRect rect(100,100,length,length);
    snake.append(rect);
    adddown();
    adddown();
    for(int i=0;i<10;i++)
        adddown();
    addfruit();
    addafruit();

    snakestate=false;
}

Widget::~Widget()
{
    delete ui;
    delete e;
}

void Widget::keyPressEvent(QKeyEvent* event)
{
    switch(event->key())
    {
    case Qt::Key_W:
        if(dir!=DOWN)
        {dir=UP;}
        break;
    case Qt::Key_S:
        if(dir!=UP)
        {dir=DOWN;}
        break;
    case Qt::Key_A:
        if(dir!=RIGHT)
        {dir=LEFT;}
        break;
    case Qt::Key_D:
        if(dir!=LEFT)
        {dir=RIGHT;}
        break;
    case Qt::Key_Space:
        if(gamestate==false)
        {
            gamestate=true;
            //启动定时器
            timer->start(outime);
        }
        else
        {
            gamestate=false;
            timer->stop();
        }
        break;
    default:
        break;
    }
}

void Widget::timeout()
{
    //eat fruit
    int t=1;
    if(snake[0].intersects(fruit))
    {
        t++;
        score++;
        addfruit();
    }

    if(snake[0].intersects(afruit))
    {
        t++;
        score++;
        addafruit();
    }

    while(t--)
    {
        switch(dir)
        {
        case UP:
            addup();
            break;
        case DOWN:
            adddown();
            break;
        case LEFT:
            addleft();
            break;
        case RIGHT:
            addright();
            break;
        default:
            break;
        }
    }
    deletelast();

    //更新得分
    QFont f("文本",30);
    scorelabel2->setNum(score);
    scorelabel2->setFont(f);
    scorelabel2->setGeometry(100,0,50,50);

    //int rand=QRandomGenerator::global()->bounded(40);
    //qDebug()<<score<<"  "<<rand;

    crash();
    if(snakestate)
    {
        timer->stop();
        gamestate=false;
        e->show();
    }

    update();
}

void Widget::addup()
{
    QPointF lefttop;
    QPointF rightbottom;

    if(snake[0].y()-length<0)
    {
        lefttop=QPointF(snake[0].x(),this->height()-length);
        rightbottom=QPointF(snake[0].x()+length,this->height());
    }
    else
    {
        lefttop=QPointF(snake[0].x(),snake[0].y()-length);
        rightbottom=QPointF(snake[0].x()+length,snake[0].y());
    }

    snake.insert(0,QRectF(lefttop,rightbottom));
}

void Widget::adddown()
{
    QPointF lefttop;
    QPointF rightbottom;

    if(snake[0].y()+length>=this->height())
    {
        lefttop=QPointF(snake[0].x(),0);
        rightbottom=QPointF(snake[0].x()+length,length);
    }

    else
    {
        lefttop=QPointF(snake[0].x(),snake[0].y()+length);
        rightbottom=QPointF(snake[0].x()+length,snake[0].y()+2*length);
    }

    snake.insert(0,QRectF(lefttop,rightbottom));
}

void Widget::addleft()
{
    QPointF lefttop;
    QPointF rightbottom;

    if(snake[0].x()-length<0)
    {
        lefttop=QPointF(this->width(),snake[0].y());
        rightbottom=QPointF(this->width()+length,snake[0].y()+length);
    }
    else
    {
        lefttop=QPointF(snake[0].x()-length,snake[0].y());
        rightbottom=QPointF(snake[0].x(),snake[0].y()+length);
    }

    snake.insert(0,QRectF(lefttop,rightbottom));
}

void Widget::addright()
{
    QPointF lefttop;
    QPointF rightbottom;

    if(snake[0].x()+length>=this->width())
    {
        lefttop=QPointF(0,snake[0].y());
        rightbottom=QPointF(length,snake[0].y()+length);
    }
    else
    {
        lefttop=QPointF(snake[0].x()+length,snake[0].y());
        rightbottom=QPointF(snake[0].x()+2*length,snake[0].y()+length);
    }

    snake.insert(0,QRectF(lefttop,rightbottom));
}

void Widget::addfruit()
{
    int x=QRandomGenerator::global()->bounded(40);
    int y=QRandomGenerator::global()->bounded(40);
    fruit=QRectF(20*x,20*y,length,length);
}

void Widget::addafruit()
{
    int x=QRandomGenerator::global()->bounded(40);
    int y=QRandomGenerator::global()->bounded(40);
    afruit=QRectF(20*x,20*y,length,length);
}

void Widget::deletelast()
{
    snake.removeLast();
}

void Widget::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    QPen pen;
    QBrush brush;

    //QPixmap pix;
    //pix.load("E:\QTproject\game\OIP-C.jpg");
    //painter.drawPixmap(0,0,474,474,pix);


    //fruit
    pen.setColor(Qt::black);
    brush.setColor(Qt::white);
    brush.setStyle(Qt::SolidPattern);
    painter.setPen(pen);
    painter.setBrush(brush);
    //painter.drawRect(fruit);
    painter.drawEllipse(fruit);
    painter.drawEllipse(afruit);

    //蛇
    pen.setColor(Qt::black);
    brush.setColor(Qt::red);
    brush.setStyle(Qt::SolidPattern);
    painter.setPen(pen);
    painter.setBrush(brush);

    for(int i=0;i<snake.length();i++)
    {
        painter.drawRect(snake[i]);
    }
}

void Widget::crash()
{
    if(dir==UP)
    {
        for(int i=1;i<snake.length();i++)
        {
            if((snake[0].x()==snake[i].x())&&(snake[0].y()-length==snake[i].y()))
            {
                snakestate=true;
                break;
            }
        }
    }
    if(dir==DOWN)
    {
        for(int i=1;i<snake.length();i++)
        {
            if((snake[0].x()==snake[i].x())&&(snake[0].y()+length==snake[i].y()))
            {
                snakestate=true;
                break;
            }
        }
    }
    if(dir==LEFT)
    {
        for(int i=1;i<snake.length();i++)
        {
            if((snake[0].x()-length==snake[i].x())&&(snake[0].y()==snake[i].y()))
            {
                snakestate=true;
                break;
            }
        }
    }
    if(dir==RIGHT)
    {
        for(int i=1;i<snake.length();i++)
        {
            if((snake[0].x()+length==snake[i].x())&&(snake[0].y()==snake[i].y()))
            {
                snakestate=true;
                break;
            }
        }
    }
}
